<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
	
	case 'addAuthor' :
		addAuthor();
		break;
		
	case 'modifyAuthor' :
		modifyAuthor();
		break;
		
	case 'deleteAuthor' :
		deleteAuthor();
		break;
	
	case 'deleteImage' :
		deleteImage();
		break;
    
	default :
	    // if action is not defined or unknown
		// move to main Author page
		header('Location: index.php');
}


function addAuthor()
{
    $nhomnc_id   = $_POST['cboGroup'];
    $matg        = $_POST['txtMatg'];
	$hotentg	 = $_POST['txtHotentg'];
	$ngaysinh    = $_POST['txtNgaysinh'];

	$arr = explode("-", $ngaysinh);
	$ngaysinh = $arr[2] . "-" . $arr[1] . "-" . $arr[0];

	$quequan     = $_POST['txtQuequan'];
 	$dv_id   	 = (int)$_POST['cboDept'];
	$chucvu		 = $_POST['txtChucvu'];
	$diachi 	= $_POST['txtDiachi'];
	$dienthoai   = $_POST['txtDienthoai'];
	//$didong 	= $_POST['txtDTDD'];
	$hocham      = $_POST['txtHocham'];
	$hocvi	     = $_POST['txtHocvi'];
	
	$didong		 = $_POST['txtDTDD'];
	$email		= $_POST['txtEmail'];
	$ngaythamgia		= $_POST['txtNgaythamgia'];
	
	$images = uploadAuthorImage('fleImage', SRV_ROOT . 'images/author/');

	$mainImage = $images['image'];
	$thumbnail = $images['thumbnail'];
	//echo $mainImage; exit();
	
	$sql1   = "INSERT INTO tacgia (matg, hotentg, ngaysinh, quequantg, dv_id, chucvu, diachi, hocham, hocvi, dienthoai, didong, email, hinhanh, hinhanh_thumb)
	          VALUES ('$matg', '$hotentg', '$ngaysinh', '$quequan', $dv_id, '$chucvu','$diachi', '$hocham', '$hocvi', '$dienthoai', '$didong', '$email', '$mainImage', '$thumbnail')";
			  //echo $ssql1; exit();

	$result1 = dbQuery($sql1);
	$sql2 	= "select id from tacgia where matg = '$matg'";
	
	$result2 = dbQuery($sql2) or die('' . mysql_error());
	
	if (mysql_num_rows($result2)<>0){
		while ($row = mysql_fetch_array($result2))
			$tg_id = $row["id"];
	}
	//$ngaythamgia1 = year($ngaythamgia). '/'.MONTH($ngaythamgia).'/'.day($ngaythamgia);
	//echo $ngaythamgia1;
	//exit();
	if ($nhomnc_id !=0)	{
		$sql3   = "INSERT INTO tacgia_nhomnc(tg_id, nhomnc_id, ngaythamgia)
	          VALUES($tg_id, $nhomnc_id, '$ngaythamgia')";
		$result1 = dbQuery($sql3);
	}
	
	header("Location: index.php?nhomnc_id=$nhomnc_id");	
}
/*
	Upload an image and return the uploaded image name 
*/

function uploadAuthorImage($inputName, $uploadDir)
{
	$image     = $_FILES[$inputName];
	$imagePath = '';
	//$thumbnailPath = '';
	
	// if a file is given
	if (trim($image['tmp_name']) != '') {
		$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];

		// generate a random new file name to avoid name conflict
		$imagePath = md5(rand() * time()) . ".$ext";
		
		list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 

		// make sure the image width does not exceed the
		// maximum allowed width
		if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
			$result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
			$imagePath = $result;
		} else {
			$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
		}	
		
		if ($result) {
			// create thumbnail
			$thumbnailPath =  md5(rand() * time()) . ".$ext";
			$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
			
			// create thumbnail failed, delete the image
			if (!$result) {
				unlink($uploadDir . $imagePath);
				$imagePath = $thumbnailPath = '';
			} else {
				$thumbnailPath = $result;
			}	
		} else {
			// the product cannot be upload / resized
			$imagePath = $thumbnailPath = '';
		}
		
	}

	return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
/*
	Modify a author
*/
function modifyAuthor()
{
	echo 'AAAAAAAAAAAAA';
	$id = (int)$_GET['id']; echo $id;
	$nhomnc_id 	= (int)$_POST['cboGroup'];
	$dv_id   	 = (int)$_POST['cboDept'];
	//$matg   	= $_GET['matg'];	
    $hotentg    = $_POST['txtHotentg'];
	$ngaysinh 	= $_POST['txtNgaysinh'];
	$quequantg  = $_POST['txtQuequan'];
	$chucvu  	= $_POST['txtChucvu'];
	$hocham 	= $_POST['txtHocham'];
	$hocvi 		= $_POST['txtHocvi'];
	$email 		= $_POST['txtEmail'];
	$dienthoai   = $_POST['txtDienthoai'];
	$didong		 = $_POST['txtDTDD'];
	

	$sql1   = "UPDATE tacgia 
               SET hotentg = '$hotentg', ngaysinh = '$ngaysinh', quequantg = '$quequantg', dv_id = $dv_id, chucvu = '$chucvu', hocham = '$hocham', hocvi = '$hocvi', email = '$email', dienthoai = '$dienthoai', didong = '$didong'
			   WHERE id = $id" ;		   
	//$result = dbQuery($sql1); 	dbQuery($sql1) ;
	
	$sql4 	= "select * from tacgia_nhomnc where tg_id = $id";
	
	$result4 = mysql_query($sql4);
	
	if (mysql_num_rows($result4)<>0){
		$sql5   = "		UPDATE tacgia_nhomnc
					   SET nhomnc_id = $nhomnc_id
					   WHERE tg_id = $id" ;	
	}else{
			if ($nhomnc_id != 0)	
				$sql5 = "INSERT INTO tacgia_nhomnc(tg_id, nhomnc_id, ngaythamgia) VALUES ($id, $nhomnc_id, now())";
		}   
	$result5 = dbQuery($sql5);
	header('Location: index.php');			  
}

/*
	Remove a author
*/
function deleteAuthor()
{/*

	if (isset($_GET['matg']) && (int)$_GET['matg'] !='') {
		$matg = $_GET['matg'];
	} else {
		header('Location: index.php');
	}
	
	// remove any references to this author from
	// tbl_order_item and tbl_cart
	$sql = "DELETE FROM tbl_order_item
	        WHERE pd_id = $matg";
	dbQuery($sql);
			
	$sql = "DELETE FROM tbl_cart
	        WHERE pd_id = $matg";	
	dbQuery($sql);
			
	// get the image name and thumbnail
	$sql = "SELECT hinhanh, hinhanh_thumb
	        FROM tbl_author
			WHERE pd_id = $matg";
			
	$result = dbQuery($sql);
	$row    = dbFetchAssoc($result);
	
	// remove the author image and thumbnail
	if ($row['hinhanh']) {
		unlink(SRV_ROOT . 'images/product/' . $row['hinhanh']);
		unlink(SRV_ROOT . 'images/product/' . $row['hinhanh_thumb']);
	}
	
	// remove the author from database;
	$sql = "DELETE FROM tbl_author 
	        WHERE pd_id = $matg";
	dbQuery($sql);
	
	header('Location: index.php?catId=' . $_GET['catId']);*/
}


/*
	Remove a author image
*/
function deleteImage()
{
	if (isset($_GET['id']) && (int)$_GET['id'] > 0) {
		$id = (int)$_GET['id'];
	} else {
		header('Location: index.php');
	}
	
	$deleted = _deleteImage($id);

	// update the image and thumbnail name in the database
	$sql = "UPDATE tacgia
			SET hinhanh = '', hinhanh_thumb = ''
			WHERE id = $id";
	dbQuery($sql);		

	header("Location: index.php?view=modify&id=$id");
}

function _deleteImage($id)
{
	// we will return the status
	// whether the image deleted successfully
	$deleted = false;
	
	$sql = "SELECT hinhanh, hinhanh_thumb 
	        FROM tacgia
			WHERE id = $id";
	$result = dbQuery($sql) or die('Cannot delete author image. ' . mysql_error());
	
	if (dbNumRows($result)) {
		$row = dbFetchAssoc($result);
		extract($row);
		
		if ($hinhanh && $hinhanh_thumb) {
			// remove the image file
			$deleted = @unlink(SRV_ROOT . "images/author/$hinhanh");
			$deleted = @unlink(SRV_ROOT . "images/author/$hinhanh_thumb");
		}
	}
	
	return $deleted;
}



?>